home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxtype1.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  12.9 KB  |  356 lines

  1. /* Copyright (C) 1990, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxtype1.h,v 1.4 2000/09/19 19:00:40 lpd Exp $ */
  20. /* Private Adobe Type 1 / Type 2 charstring interpreter definitions */
  21.  
  22. #ifndef gxtype1_INCLUDED
  23. #  define gxtype1_INCLUDED
  24.  
  25. #include "gscrypt1.h"
  26. #include "gstype1.h"
  27. #include "gxop1.h"
  28.  
  29. /* This file defines the structures for the state of a Type 1 / */
  30. /* Type 2 charstring interpreter. */
  31.  
  32. /*
  33.  * Because of oversampling, one pixel in the Type 1 interpreter may
  34.  * correspond to several device pixels.  This is also true of the hint data,
  35.  * since the CTM reflects the transformation to the oversampled space.
  36.  * To help keep the font level hints separated from the character level hints,
  37.  * we store the scaling factor separately with each set of hints.
  38.  */
  39. typedef struct pixel_scale_s {
  40.     fixed unit;            /* # of pixels per device pixel */
  41.     fixed half;            /* unit / 2 */
  42.     int log2_unit;        /* log2(unit / fixed_1) */
  43. } pixel_scale;
  44. typedef struct point_scale_s {
  45.     pixel_scale x, y;
  46. } point_scale;
  47.  
  48. #define set_pixel_scale(pps, log2)\
  49.   (pps)->unit = ((pps)->half = fixed_half << ((pps)->log2_unit = log2)) << 1
  50. #define scaled_rounded(v, pps)\
  51.   (((v) + (pps)->half) & -(pps)->unit)
  52.  
  53. /* ------ Font level hints ------ */
  54.  
  55. /* Define the standard stem width tables. */
  56. /* Each table is sorted, since the StemSnap arrays are sorted. */
  57. #define max_snaps (1 + max_StemSnap)
  58. typedef struct {
  59.     int count;
  60.     fixed data[max_snaps];
  61. } stem_snap_table;
  62.  
  63. /* Define the alignment zone structure. */
  64. /* These are in device coordinates also. */
  65. #define max_a_zones (max_BlueValues + max_OtherBlues)
  66. typedef struct {
  67.     int is_top_zone;
  68.     fixed v0, v1;        /* range for testing */
  69.     fixed flat;            /* flat position */
  70. } alignment_zone;
  71.  
  72. /* Define the structure for hints that depend only on the font and CTM, */
  73. /* not on the individual character.  Eventually these should be cached */
  74. /* with the font/matrix pair. */
  75. typedef struct font_hints_s {
  76.     bool axes_swapped;        /* true if x & y axes interchanged */
  77.     /* (only set if using hints) */
  78.     bool x_inverted, y_inverted;    /* true if axis is inverted */
  79.     bool use_x_hints;        /* true if we should use hints */
  80.     /* for char space x coords (vstem) */
  81.     bool use_y_hints;        /* true if we should use hints */
  82.     /* for char space y coords (hstem) */
  83.     point_scale scale;        /* oversampling scale */
  84.     stem_snap_table snap_h;    /* StdHW, StemSnapH */
  85.     stem_snap_table snap_v;    /* StdVW, StemSnapV */
  86.     fixed blue_fuzz, blue_shift;    /* alignment zone parameters */
  87.     /* in device pixels */
  88.     bool suppress_overshoot;    /* (computed from BlueScale) */
  89.     int a_zone_count;        /* # of alignment zones */
  90.     alignment_zone a_zones[max_a_zones];    /* the alignment zones */
  91. } font_hints;
  92.  
  93. /* ------ Character level hints ------ */
  94.  
  95. /*
  96.  * Define the stem hint tables.  Each stem hint table is kept sorted.
  97.  * Stem hints are in device coordinates.  We have to retain replaced hints
  98.  * so that we can make consistent rounding choices for stem edges.
  99.  * This is clunky, but I don't see any other way to do it.
  100.  *
  101.  * The Type 2 charstring documentation says that the total number of hints
  102.  * is limited to 96, but since we store horizontal and vertical hints
  103.  * separately, we must set max_stems large enough to allow either one to
  104.  * get this big.
  105.  */
  106. #define max_total_stem_hints 96
  107. #define max_stems 96
  108. typedef struct {
  109.     fixed v0, v1;        /* coordinates (widened a little) */
  110.     fixed dv0, dv1;        /* adjustment values */
  111.     ushort index;        /* sequential index of hint, */
  112.     /* needed for implementing hintmask */
  113.     ushort active;        /* true if hint is active (hintmask) */
  114. } stem_hint;
  115. typedef struct {
  116.     int count;
  117.     int current;        /* cache cursor for search */
  118.     /*
  119.      * For dotsection and Type 1 Charstring hint replacement,
  120.      * we store active hints at the bottom of the table, and
  121.      * replaced hints at the top.
  122.      */
  123.     int replaced_count;        /* # of replaced hints at top */
  124.     stem_hint data[max_stems];
  125. } stem_hint_table;
  126.  
  127. /* ------ Interpreter state ------ */
  128.  
  129. /* Define the control state of the interpreter. */
  130. /* This is what must be saved and restored */
  131. /* when calling a CharString subroutine. */
  132. typedef struct {
  133.     const byte *ip;
  134.     crypt_state dstate;
  135.     gs_const_string char_string;    /* original CharString or Subr, */
  136.     /* for GC */
  137. } ip_state;
  138.  
  139. /* Get the next byte from a CharString.  It may or may not be encrypted. */
  140. #define charstring_this(ch, state, encrypted)\
  141.   (encrypted ? decrypt_this(ch, state) : ch)
  142. #define charstring_next(ch, state, chvar, encrypted)\
  143.   (encrypted ? (chvar = decrypt_this(ch, state),\
  144.         decrypt_skip_next(ch, state)) :\
  145.    (chvar = ch))
  146. #define charstring_skip_next(ch, state, encrypted)\
  147.   (encrypted ? decrypt_skip_next(ch, state) : 0)
  148.  
  149. #ifndef gx_path_DEFINED
  150. #  define gx_path_DEFINED
  151. typedef struct gx_path_s gx_path;
  152. #endif
  153.  
  154. #ifndef segment_DEFINED
  155. #  define segment_DEFINED
  156. typedef struct segment_s segment;
  157. #endif
  158.  
  159. /* This is the full state of the Type 1 interpreter. */
  160. #define ostack_size 48        /* per Type 2 documentation */
  161. #define ipstack_size 10        /* per documentation */
  162. struct gs_type1_state_s {
  163.     /* The following are set at initialization */
  164.     gs_font_type1 *pfont;    /* font-specific data */
  165.     gs_imager_state *pis;    /* imager state */
  166.     gx_path *path;        /* path for appending */
  167.     bool charpath_flag;        /* false if show, true if charpath */
  168.     int paint_type;        /* 0/3 for fill, 1/2 for stroke */
  169.     void *callback_data;
  170.     fixed_coeff fc;        /* cached fixed coefficients */
  171.     float flatness;        /* flatness for character curves */
  172.     point_scale scale;        /* oversampling scale */
  173.     font_hints fh;        /* font-level hints */
  174.     gs_fixed_point origin;    /* character origin */
  175.     /* The following are updated dynamically */
  176.     fixed ostack[ostack_size];    /* the Type 1 operand stack */
  177.     int os_count;        /* # of occupied stack entries */
  178.     ip_state ipstack[ipstack_size + 1];        /* control stack */
  179.     int ips_count;        /* # of occupied entries */
  180.     int init_done;        /* -1 if not done & not needed, */
  181.                 /* 0 if not done & needed, 1 if done */
  182.     bool sb_set;        /* true if lsb is preset */
  183.     bool width_set;        /* true if width is set (for seac parts) */
  184.     bool have_hintmask;        /* true if using a hint mask */
  185.     /* (Type 2 charstrings only) */
  186.     int num_hints;        /* number of hints (Type 2 only) */
  187.     gs_fixed_point lsb;        /* left side bearing (char coords) */
  188.     gs_fixed_point width;    /* character width (char coords) */
  189.     int seac_accent;        /* accent character code for seac, or -1 */
  190.     fixed save_asb;        /* save seac asb */
  191.     gs_fixed_point save_lsb;    /* save seac accented lsb */
  192.     gs_fixed_point save_adxy;    /* save seac adx/ady */
  193.     fixed asb_diff;        /* save_asb - save_lsb.x, */
  194.                 /* needed to adjust Flex endpoint */
  195.     gs_fixed_point adxy;    /* seac accent displacement, */
  196.                 /* needed to adjust currentpoint */
  197.     gs_fixed_point position;    /* save unadjusted position */
  198.                 /* when returning temporarily to caller */
  199.     int flex_path_state_flags;    /* record whether path was open */
  200.                 /* at start of Flex section */
  201. #define flex_max 8
  202.     gs_fixed_point flex_points[flex_max];    /* points for Flex */
  203.     int flex_count;
  204.     int ignore_pops;        /* # of pops to ignore (after */
  205.                 /* a known othersubr call) */
  206.     /* The following are set dynamically. */
  207. #define dotsection_in 0
  208. #define dotsection_out (-1)
  209.     int dotsection_flag;    /* 0 if inside dotsection, */
  210.     /* -1 if outside */
  211.     bool vstem3_set;        /* true if vstem3 seen */
  212.     gs_fixed_point vs_offset;    /* device space offset for centering */
  213.     /* middle stem of vstem3 */
  214.     int hints_initial;        /* hints applied to initial point */
  215.     /* of subpath */
  216.     gs_fixed_point unmoved_start;    /* original initial point of subpath */
  217.     segment *hint_next;        /* last segment where hints have */
  218.     /* been applied, 0 means none of */
  219.     /* current subpath has been hinted */
  220.     int hints_pending;        /* hints applied to end of hint_next */
  221.     gs_fixed_point unmoved_end;    /* original hint_next->pt */
  222.     stem_hint_table hstem_hints;    /* horizontal stem hints */
  223.     stem_hint_table vstem_hints;    /* vertical stem hints */
  224.     fixed transient_array[32];    /* Type 2 transient array, */
  225.     /* will be variable-size someday */
  226. };
  227.  
  228. extern_st(st_gs_type1_state);
  229. #define public_st_gs_type1_state() /* in gxtype1.c */\
  230.   gs_public_st_composite(st_gs_type1_state, gs_type1_state, "gs_type1_state",\
  231.     gs_type1_state_enum_ptrs, gs_type1_state_reloc_ptrs)
  232.  
  233. /* ------ Shared Type 1 / Type 2 interpreter fragments ------ */
  234.  
  235. /* Define a pointer to the charstring interpreter stack. */
  236. typedef fixed *cs_ptr;
  237.  
  238. /* Clear the operand stack. */
  239. /* The cast avoids compiler warning about a "negative subscript." */
  240. #define CLEAR_CSTACK(cstack, csp)\
  241.   (csp = (cs_ptr)(cstack) - 1)
  242.  
  243. /* Copy the operand stack out of the saved state. */
  244. #define INIT_CSTACK(cstack, csp, pcis)\
  245.   BEGIN\
  246.     if ( pcis->os_count == 0 )\
  247.       CLEAR_CSTACK(cstack, csp);\
  248.     else {\
  249.       memcpy(cstack, pcis->ostack, pcis->os_count * sizeof(fixed));\
  250.       csp = &cstack[pcis->os_count - 1];\
  251.     }\
  252.   END
  253.  
  254. /* Decode a 1-byte number. */
  255. #define decode_num1(var, c)\
  256.   (var = c_value_num1(c))
  257. #define decode_push_num1(csp, c)\
  258.   (*++csp = int2fixed(c_value_num1(c)))
  259.  
  260. /* Decode a 2-byte number. */
  261. #define decode_num2(var, c, cip, state, encrypted)\
  262.   BEGIN\
  263.     uint c2 = *cip++;\
  264.     int cn = charstring_this(c2, state, encrypted);\
  265. \
  266.     var = (c < c_neg2_0 ? c_value_pos2(c, 0) + cn :\
  267.        c_value_neg2(c, 0) - cn);\
  268.     charstring_skip_next(c2, state, encrypted);\
  269.   END
  270. #define decode_push_num2(csp, c, cip, state, encrypted)\
  271.   BEGIN\
  272.     uint c2 = *cip++;\
  273.     int cn;\
  274. \
  275.     cn = charstring_this(c2, state, encrypted);\
  276.     if ( c < c_neg2_0 )\
  277.       { if_debug2('1', "[1] (%d)+%d\n", c_value_pos2(c, 0), cn);\
  278.         *++csp = int2fixed(c_value_pos2(c, 0) + (int)cn);\
  279.       }\
  280.     else\
  281.       { if_debug2('1', "[1] (%d)-%d\n", c_value_neg2(c, 0), cn);\
  282.         *++csp = int2fixed(c_value_neg2(c, 0) - (int)cn);\
  283.       }\
  284.     charstring_skip_next(c2, state, encrypted);\
  285.   END
  286.  
  287. /* Decode a 4-byte number, but don't push it, because Type 1 and Type 2 */
  288. /* charstrings scale it differently. */
  289. #if arch_sizeof_long > 4
  290. #  define sign_extend_num4(lw)\
  291.      lw = (lw ^ 0x80000000L) - 0x80000000L
  292. #else
  293. #  define sign_extend_num4(lw) DO_NOTHING
  294. #endif
  295. #define decode_num4(lw, cip, state, encrypted)\
  296.   BEGIN\
  297.     int i;\
  298.     uint c4;\
  299. \
  300.     lw = 0;\
  301.     for ( i = 4; --i >= 0; )\
  302.       { charstring_next(*cip, state, c4, encrypted);\
  303.         lw = (lw << 8) + c4;\
  304.     cip++;\
  305.       }\
  306.     sign_extend_num4(lw);\
  307.   END
  308.  
  309. /* ------ Shared Type 1 / Type 2 charstring utilities ------ */
  310.  
  311. void gs_type1_finish_init(P2(gs_type1_state * pcis, is_ptr ps));
  312.  
  313. int gs_type1_sbw(P5(gs_type1_state * pcis, fixed sbx, fixed sby,
  314.             fixed wx, fixed wy));
  315.  
  316. /* blend returns the number of values to pop. */
  317. int gs_type1_blend(P3(gs_type1_state *pcis, fixed *csp, int num_results));
  318.  
  319. int gs_type1_seac(P4(gs_type1_state * pcis, const fixed * cstack,
  320.              fixed asb_diff, ip_state * ipsp));
  321.  
  322. int gs_type1_endchar(P1(gs_type1_state * pcis));
  323.  
  324. /* ----- Interface between main Type 1 interpreter and hint routines ----- */
  325.  
  326. /* Font level hints */
  327. void reset_font_hints(P2(font_hints *, const gs_log2_scale_point *));
  328. void compute_font_hints(P4(font_hints *, const gs_matrix_fixed *,
  329.                const gs_log2_scale_point *,
  330.                const gs_type1_data *));
  331.  
  332. /* Character level hints */
  333. void reset_stem_hints(P1(gs_type1_state *)), update_stem_hints(P1(gs_type1_state *)),
  334.      type1_replace_stem_hints(P1(gs_type1_state *)),
  335. #define replace_stem_hints(pcis)\
  336.   (apply_path_hints(pcis, false),\
  337.    type1_replace_stem_hints(pcis))
  338.      type1_apply_path_hints(P3(gs_type1_state *, bool, gx_path *)),
  339. #define apply_path_hints(pcis, closing)\
  340.   type1_apply_path_hints(pcis, closing, pcis->path)
  341.      type1_do_hstem(P4(gs_type1_state *, fixed, fixed,
  342.                const gs_matrix_fixed *)),
  343. #define type1_hstem(pcis, y, dy)\
  344.   type1_do_hstem(pcis, y, dy, &(pcis)->pis->ctm)
  345.       type1_do_vstem(P4(gs_type1_state *, fixed, fixed,
  346.             const gs_matrix_fixed *)),
  347. #define type1_vstem(pcis, x, dx)\
  348.   type1_do_vstem(pcis, x, dx, &(pcis)->pis->ctm)
  349.       type1_do_center_vstem(P4(gs_type1_state *, fixed, fixed,
  350.                    const gs_matrix_fixed *));
  351.  
  352. #define center_vstem(pcis, x0, dx)\
  353.   type1_do_center_vstem(pcis, x0, dx, &(pcis)->pis->ctm)
  354.  
  355. #endif /* gxtype1_INCLUDED */
  356.